
//
//  LJHListViewController.m
//  HEARME
//
//  Created by liujinhui on 16/1/21.
//  Copyright © 2016年 dllo. All rights reserved.
//

#import "LJHListViewController.h"
#import "LJHAppTools.h"
#import "LJHTopFirstKResultsModel.h"
#import "LJHTopModel.h"
#import "LJHFocusModel.h"
#import "LJHChildListModel.h"
#import "UIImageView+WebCache.h"
#import "LJHListTableViewCell.h"
#import "LJHTopFirstKResultsModel.h"
#import "LJHListDetailViewController.h"

@interface LJHListViewController ()<UITableViewDelegate, UITableViewDataSource>

@property(nonatomic,retain)NSMutableArray *titleArray;
@property(nonatomic,retain)NSMutableArray *topListArray;
@property(nonatomic, retain)UITableView *myTable;
@property(nonatomic,retain)NSMutableArray *imageArray;
@property(nonatomic,retain)UIScrollView *myHeadScrollView;
@property(nonatomic,retain)NSMutableArray *firstArray;
@property(nonatomic,retain)NSMutableArray *arrMin;
@property(nonatomic,retain)NSMutableArray *FBarray;

@end

@implementation LJHListViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.

    self.myTable = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, WIDTH, HEIGHT-150)];
    self.myTable.delegate = self;
    self.myTable.dataSource = self;
    [self.view addSubview:self.myTable];
    [self.myTable registerClass:[LJHListTableViewCell class] forCellReuseIdentifier:@"cell"];
    [self createHeadScrollView];
    self.myTable.tableHeaderView = self.myHeadScrollView;
    
    [self getData];
    
}

#pragma mark - tableview协议方法

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    
    NSMutableArray *array = [self.topListArray objectAtIndex:section];
    
    return array.count;
    
}

-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    
    return self.titleArray.count;
    
}

-(NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section
{
    
    return [self.titleArray objectAtIndex:section];
    
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    
    return 100;
    
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    
    LJHListTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell"];
    if (self.topListArray.count!=0) {
        
    NSMutableArray *array = [self.topListArray objectAtIndex:indexPath.section];
    LJHTopModel *topModel = [array objectAtIndex:indexPath.row];
    cell.topModel = topModel;
        
    }
    if (self.FBarray.count!=0) {
        
        NSMutableArray *arr = [self.FBarray objectAtIndex:indexPath.section];
        NSMutableArray *arrMin = [arr objectAtIndex:indexPath.row];
        cell.arrFirst = arrMin;
        
    }
    return cell;

}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    
    NSMutableArray *array = [self.topListArray objectAtIndex:indexPath.section];

    LJHTopModel *model = [array objectAtIndex:indexPath.row];
   
    [self.myPushAll push:model.key contentType:model.contentType];
    
}

#pragma mark - 解析数据

-(void)getData
{
    
    self.titleArray = [NSMutableArray array];
    self.topListArray = [NSMutableArray array];
    self.FBarray = [NSMutableArray array];

    [LJHAppTools afnGetRequest:@"http://mobile.ximalaya.com/mobile/discovery/v2/rankingList/group?channel=and-d3&device=android&includeActivity=true&includeSpecial=true&scale=2&version=4.3.44.2" block:^(id result)
     
    {
       NSMutableArray *dataArray = [result objectForKey:@"datas"];
        for (NSMutableDictionary *dic in dataArray) {
            
            [self.titleArray addObject:[dic objectForKey:@"title"]];//title 节目榜单 主播榜单
            
            NSMutableArray *listArray = [dic objectForKey:@"list"];
            NSMutableArray *array = [NSMutableArray array];
            
            self.firstArray = [NSMutableArray array];

            for (NSMutableDictionary *topDic in listArray) {
                
                LJHTopModel *model = [LJHTopModel modelWithDictionary:topDic];
                
                [array addObject:model]; //每个榜单arr（两个大榜中的小榜单）
                
                NSMutableArray *arr = [topDic objectForKey:@"firstKResults"];
                self.arrMin = [NSMutableArray array];
                for (NSDictionary *dic in arr) {
                    
                    LJHTopFirstKResultsModel *fModel = [LJHTopFirstKResultsModel modelWithDictionary:dic];
                    [self.arrMin addObject:fModel]; //小榜单中的前三
                    
                }
                
                [self.firstArray addObject:self.arrMin]; //firstKResults小榜单
                
            }
            
            [self.FBarray addObject:self.firstArray]; //两个大榜单中的小榜单
            
            [self.topListArray addObject:array]; //每个榜单arr（两个大榜中的小榜单）

        }

        //轮播图数据
        LJHFocusModel *focusModel = [LJHFocusModel modelWithDictionary:[result objectForKey:@"focusImages"]];
        
        self.imageArray = [NSMutableArray arrayWithArray:focusModel.list];
        
        self.myHeadScrollView.contentSize = CGSizeMake(WIDTH*self.imageArray.count, WIDTH/2);
        for (int i = 0; i < self.imageArray.count; i++) {
            
            UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake(i*WIDTH, 0, WIDTH, WIDTH/2)];
            [imageView sd_setImageWithURL:[NSURL URLWithString:((LJHChildListModel *)[self.imageArray objectAtIndex:i]).pic] placeholderImage:[UIImage imageNamed:@"1024"]];
            [self.myHeadScrollView addSubview:imageView];
            [imageView release];
            
        }
        
        [_myHeadScrollView release];
        
        [self.myTable reloadData];

       
        
    } error:^(NSError *error) {
        
        UIAlertController *alertC = [UIAlertController alertControllerWithTitle:@"您的设备无网络访问" message:nil preferredStyle:UIAlertControllerStyleAlert];
        
        [self presentViewController:alertC animated:YES completion:^{
            
        }];
        
        UIAlertAction *cancelButton = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:^(UIAlertAction * _Nonnull action) {
        }];
        [alertC addAction:cancelButton];
        
    }];
    
    
}

#pragma mark - 创建headScrollView

-(void)createHeadScrollView
{
    
    self.myHeadScrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, WIDTH, WIDTH/2)] ;
    self.myHeadScrollView.backgroundColor = [UIColor whiteColor];
    self.myHeadScrollView.pagingEnabled = YES;
    
    
    
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
